﻿@{
    ViewBag.Title = "调拨管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@section Contents{
    <style>

        .layui-form-label {
            width: auto;
        }

        .layui-table-cell {
            height: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.ToWarehouse}}：</label>
                        <div class="layui-input-inline">
                            <select id="selWarehouse" lay-filter="selWarehouse">
                                <option value="">所有仓库</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkorderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" id="txtWorkOrderNo" placeholder="请输入工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.AllocateOrderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtAllocateOrder" placeholder="请输入调拨单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form'], function () {
                var table = layui.table,
                    form = layui.form;
                var options = {
                    elem: '#table'
                    , url: '/AllocateOrder/QueryAllocateOrders'
                    , method: "post"
                    , where: { toWarehouseCode: "", workOrderNo: "", allocateNo: "" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { type: 'numbers', title: '序号', width: 60 }
                        , { field: 'AllocateNo', title: '调拨单号', minWidth: 130 }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 200 }
                        , { field: 'FromWarehouse', title: '发出仓库', minWidth: 150 }
                        , { field: 'ToWarehouse', title: '接收仓库', minWidth: 150 }
                        , { field: 'AllocateTimeStr', title: '调拨时间', minWidth: 150 }
                        , {
                            title: '操作', minWidth: 230, fixed: 'right', templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-warn" onclick="AllocateDateilMgr(\'' + d.PkId + "','" + d.AllocateNo + '\')"><i class="layui-icon">&#xe621;</i>查看详情</button>\
                                           <button class="layui-btn layui-btn-sm layui-btn-warn" onclick="asyncBatch(\''+ d.AllocateNo + '\')" > <i class="layui-icon">&#xe666;</i>同步批次</button >';
                                           //<button class="layui-btn layui-btn-sm layui-btn-warn" onclick="allocateInbound(\''+ d.AllocateNo + '\')" > <i class="layui-icon">&#xe857;</i>收货</button >';
                                return str;
                            }
                        }
                    ]]
                };

                initWarehouse();
                function initWarehouse() {
                    $.ajax({
                        url: '/Warehouse/GetAllWarehouse',
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res)
                            if (+ res.ErrCode === 200) {
                                appendToSelect(res.Data)
                            }
                        }
                    })
                }

                function appendToSelect(obj) {
                    if (obj && obj.length) {
                        for (let i = 0; i < obj.length; i++) {
                            let opt = obj[i];
                            let option = new Option(opt.WarehouseName, opt.WarehouseCode);
                            $('#selWarehouse').append(option);
                        }
                        form.render('select');
                    }
                }

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.toWarehouseCode = $("#selWarehouse").val() == "0" ? "" : $("#selWarehouse").val();
                    options.where.workOrderNo = $("#txtWorkOrderNo").val();
                    options.where.allocateNo = $("#txtAllocateOrder").val();
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function AllocateDateilMgr(id, allocateNo) {
            parent.layui.index.openTabsPage("/AllocateOrder/AllocateOrderDetail/?allocateNo=" + allocateNo, "调拨单详情(" + allocateNo + ")");
        }

        function allocateInbound(allocateNo) {
            parent.layui.index.openTabsPage("/AllocateOrder/AllocateInbound?allocateNo=" + allocateNo, "调拨收货(" + allocateNo + ")");
        }

        function asyncBatch(allocateNo) {
            var loadId = layui.layer.load();
            $.ajax({
                url: "/AllocateOrder/AsyncEsbAllocateOrders",
                data: {
                    allocateNo: allocateNo
                },
                complete: function () {
                    layui.layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layui.layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    layui.layer.msg("同步结束！", { icon: 6 });
                }
            });
        }
    </script>
}
